<?php
$this->Html->addCrumb('Budgets',"/admin/budgets/main");
$this->Html->addCrumb('Budget Groups and Items');
$this->Html->scriptStart(array('inline' => false));
echo '
      $(function(){
         $(".deleteLink").click(function(event){
          var $link = $(this);
          $.TWS_Overlay("confirm", {title : "Delete confirmation", message : "Are you sure you want to delete this budget? <br/><br/>Please note all groups and items associated with this template will also be deleted!", buttons : {ok :{onclick : function () { $.TWS_Overlay("hide"); location.href=$link.attr("href");}}}});
          event.preventDefault();
        });

        $(".displayContent").cluetip({splitTitle: "|", cluetipClass: "tws", dropShadow : false, positionBy : "mouse", cursor : "default"});

        $("#addGroup").live("click", function(e){
          e.preventDefault();
          $("#BudgetGroupsAdd").dialog({modal: true, width:650, title :"Add Budget Group"});
        });

        $("#addItem").live("click",function(e){
          e.preventDefault();
          $("#BudgetItemsAdd").dialog({modal: true, width:650, title :"Add Budget Item"});
        });

        $("#BudgetId").change(function(){
          $.TWS_Overlay("show");
            $.ajax({
              url: "/admin/budgets/details_js",
              data : {id : $(this).val()},
              cache : false,
              success: function(data){
                $("#BudgetResults").html(data);
                $.TWS_Overlay("hide");},
              dataType: "html"
          });
        });
        //Initialization
          if($("#BudgetId").val()){
            $("#BudgetId").trigger("change");
          }
      });
       ';
$this->Html->scriptEnd();
?>

<style type="text/css">
  table.budget {
    width: 80%;
}
table.budget tr td {
  padding: 10px;
}

table.budget tr td:nth-child(1) {
  padding-left: 30px;
}

table.budget th {
  font-size: 1.2em;
  color:rgb(124,158,146);
}
table.budget tr.budgetGroup td {
  font-style: italic;
  font-weight: bold;
  font-size: 1.1em;
  color:#0981AC;
  padding-left: 15px;
}

table.budget td {
    color: #87876a;
    border: 0;
}

table.budget tbody tr:nth-child(even):not(.budgetGroup) {
   background-color: rgb(240,240,240);
}

#BudgetGroupsAdd, #BudgetItemsAdd { display: none;}

#budgetAdditions {margin-bottom: 20px;}

</style>

<h1>Budget Groups and Items</h1>
<?php echo $this->Session->flash();?>

<?php
echo $this->TWSForm->create('Budget',  array(
    'inputDefaults' => array(
        'div' => array('class'=>'row')),
        'class'=>'inlineForm',
        'type'=>'file'));
echo $this->TWSForm->input('id', array('name'=>'BudgetId', 'type'=>'select', 'label'=>'Select a Budget Template', 'options'=>$templateOptions));
echo $this->TWSForm->end();
?>


<div id="BudgetResults"></div>

<div id="BudgetGroupsAdd">
  <?php //TODO - Implement custom action in BudgetGroupsController
    echo $this->TWSForm->create('BudgetGroups',  array(
        'inputDefaults' => array(
            'div' => array('class'=>'row')),
            'class'=>'inlineForm',
            'id'=>'BudgetGroupsAdminAddForm',
            'action'=>'add',
            'type'=>'file'));
    echo $this->TWSForm->input('budget_id', array('name'=>'BudgetGroupsBudgetId', 'type'=>'select', 'label'=>'<span class="required">*</span>Budget template', 'options'=>$templateOptions));
    echo $this->TWSForm->input('name', array('name'=>'BudgetGroupsName', 'size'=>'63', 'type'=>'text', 'label'=>'<span class="required">*</span>Group name'));
    echo $this->TWSForm->input('percentage', array('name'=>'BudgetGroupsPercentage','type'=>'text','label'=>'<span class="required">*</span>Percentage of total'));
    echo $this->TWSForm->end( array("label"=>"Add", "class"=>"button"));
?>
</div>

<div id="BudgetItemsAdd">
  <?php //TODO - Implement custom action in BudgetItemsController
  //TODO - $groupOptions has to be determined
  $groupOptions = array();
    echo $this->TWSForm->create('BudgetItems',  array(
        'inputDefaults' => array(
            'div' => array('class'=>'row')),
            'class'=>'inlineForm',
            'id'=>'BudgetItemsAdminAddForm',
            'action'=>'add',
            'type'=>'file'));
    echo $this->TWSForm->input('budget_group_id', array('name'=>'BudgetItemsBudgetGroupId', 'type'=>'select', 'label'=>'<span class="required">*</span>Budget group', 'options'=>$groupOptions));
    echo $this->TWSForm->input('name', array('name'=>'BudgetItemsName', 'size'=>'63', 'type'=>'text', 'label'=>'<span class="required">*</span>Item name'));
    echo $this->TWSForm->end( array("label"=>"Add", "class"=>"button"));
?>
</div>

<!--
<table class="budget">
  <thead>
    <tr>
      <th>Item / Category</th>
      <th>Budgeted Amount</th>
      <th>Amount Spent</th>
    </tr>
  </thead>
  <tbody>
    <tr class="budgetGroup">
      <td colspan="3">Reception (50%)</td>
    </tr>
    <tr>
      <td>Cake</td>
      <td>$1000</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Food and Service</td>
      <td>$500</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Beverages</td>
      <td>$500</td>
      <td>&nbsp;</td>
    </tr>
  </tbody>
<tbody>
    <tr class="budgetGroup">
      <td colspan="3">Music (10%)</td>
    </tr>
    <tr>
      <td>Ceremony Musicians</td>
      <td>$1000</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Reception Band</td>
      <td>$500</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>DJ</td>
      <td>$500</td>
      <td>&nbsp;</td>
    </tr>

  </tbody>
</table>
-->






<?php if(!empty($budgets)) { ?>

   <table class="results" >
    <thead>
      <tr>
        <th width="40%"><?php echo $this->Paginator->sort('description','Description')?></th>
        <th width="10%"><?php echo $this->Paginator->sort('modified','Modified')?></th>
        <th width="15%">Actions</th>
      <tr>
   </thead>
   <tbody>
      <?php foreach ($budgets as $budget) {
        $rec = $budget['Budget'];
        $editLink = $this->Html->link('Edit', array('controller' => 'budgets', 'action' => 'edit', $rec['id']), array("title"=>"Edit this record"));
        $deleteLink = $this->Html->link('Delete', array('controller' => 'budgets', 'action' => 'delete', $rec['id']), array("title"=>"Delete this record", "class"=>'deleteLink'));
        $description = $this->Html->displayPreview(($rec['description']));
        $modified = new DateTime($rec['modified']);
        echo '<tr>';
        echo '<td>'.nl2br($description).'</td>';
        echo '<td>'.$modified->format('m/d/Y').'</td>';
        echo "<td>$editLink&nbsp;&nbsp;$deleteLink</td>";
        echo '</tr>';
      }?>
   </tbody>
   </table>
  
<?php echo $this->element('pagination'); ?>

 <?php } ?>

